'''
''' Override Validate method. If record exists, do not add the record and throw
''' custom error message to the user.
''' When an exception is thrown in the Validate() sub, it is caught in the SaveButton_Click handler.
''' SaveButton_Click handler calls RegisterJScriptAlert to report the error message to the user.
''' Throwing an exception ensures that the record will not be saved. If the exception is
''' not thrown the record is saved.
'''
Public Overrides Sub Validate()
' Create a whereStr
Dim whereStr As String = ${${Table Name}ClassName}.${Primary Key}.InternalName & "='" & Me.${Primary Key Control}.Text & "'"
Dim rec As ${${Table Name}RecordClassName} = Nothing
' Check if the record exists
rec = ${${Table Name}ClassName}.GetRecord(whereStr)
' If record exists then throw custom error message to the user
If (Not IsNothing(rec)) Then
' Set custom error message
Throw New Exception("Record with this primary key already exists.")
End If
End Sub
|